package com.pzb.web.controller.market;

import java.util.List;

import com.pzb.goods.domain.GdGoods;
import com.pzb.goods.service.IGdGoodsService;
import com.pzb.wechat.service.IWechatAppletService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pzb.common.annotation.Log;
import com.pzb.common.core.controller.BaseController;
import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.enums.BusinessType;
import com.pzb.market.domain.MkDiscount;
import com.pzb.market.service.IMkDiscountService;
import com.pzb.common.core.page.TableDataInfo;

/**
 * 店铺活动Controller
 * 
 * @author pzb
 * @date 2021-10-12
 */
@RestController
@RequestMapping("/market/discount")
public class MkDiscountController extends BaseController
{
    @Autowired
    private IMkDiscountService mkDiscountService;
    @Autowired
    private IGdGoodsService gdGoodsService;
    @Autowired
    private IWechatAppletService wechatAppletService;

    /**
     * 查询店铺活动列表
     */
    @PreAuthorize("@ss.hasPermi('market:discount:list')")
    @GetMapping("/list")
    public TableDataInfo list(MkDiscount mkDiscount)
    {
        startPage();
        mkDiscount.setExtAppid(wechatAppletService.getExtAppid());
        List<MkDiscount> list = mkDiscountService.selectMkDiscountList(mkDiscount);
        return getDataTable(list);
    }

    /**
     * 查询活动商品表
     */
    @PreAuthorize("@ss.hasPermi('market:discount:list')")
    @GetMapping(value = "/getGoodsList")
    public TableDataInfo getGoodsList(MkDiscount mkDiscount)
    {
        startPage();
        List<GdGoods> list = gdGoodsService.selectGdGoodsListByDiscountId(mkDiscount.getId());
        return getDataTable(list);
    }

    /**
     * 查询活动赠品
     */
    @PreAuthorize("@ss.hasPermi('market:discount:list')")
    @GetMapping(value = "/getGiftList")
    public TableDataInfo getGiftList(MkDiscount mkDiscount)
    {
        startPage();
        List<GdGoods> list = gdGoodsService.selectGiftListByDiscountId(mkDiscount.getId());
        return getDataTable(list);
    }




    /**
     * 获取店铺活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('market:discount:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mkDiscountService.selectMkDiscountById(id));
    }

    /**
     * 新增店铺活动
     */
    @PreAuthorize("@ss.hasPermi('market:discount:add')")
    @Log(title = "店铺活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MkDiscount mkDiscount)
    {
        String extAppid = wechatAppletService.getExtAppid();
        if("1".equals(extAppid)){
            return AjaxResult.error("您还没有小程序!");
        }
        mkDiscount.setExtAppid(extAppid);
        return toAjax(mkDiscountService.insertMkDiscount(mkDiscount));
    }

    /**
     * 修改店铺活动
     */
    @PreAuthorize("@ss.hasPermi('market:discount:edit')")
    @Log(title = "店铺活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MkDiscount mkDiscount)
    {
        return toAjax(mkDiscountService.updateMkDiscount(mkDiscount));
    }

    /**
     * 删除店铺活动
     */
    @PreAuthorize("@ss.hasPermi('market:discount:remove')")
    @Log(title = "店铺活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mkDiscountService.deleteMkDiscountByIds(ids));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('market:discount:edit')")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody MkDiscount mkDiscount)
    {
        return toAjax(mkDiscountService.updateMkDiscount(mkDiscount));
    }

    /**
     * 添加活动商品
     */
    @PreAuthorize("@ss.hasPermi('market:discount:add')")
    @Log(title = "店铺活动", businessType = BusinessType.INSERT)
    @PostMapping("/addGoods")
    public AjaxResult addGoods(@RequestBody MkDiscount mkDiscount)
    {
        return toAjax(mkDiscountService.insertMkDiscountGoods(mkDiscount));
    }

    /**
     * 删除活动商品
     */
    @PreAuthorize("@ss.hasPermi('market:discount:remove')")
    @Log(title = "店铺活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/delDiscountGoods/{ids}")
    public AjaxResult delDiscountGoods(@PathVariable Long[] ids)
    {
        return toAjax(mkDiscountService.deleteMkDiscountGoodsByIds(ids));
    }

    /**
     * 添加活动赠品
     */
    @PreAuthorize("@ss.hasPermi('market:discount:add')")
    @Log(title = "店铺活动", businessType = BusinessType.INSERT)
    @PostMapping("/addGift")
    public AjaxResult addGift(@RequestBody MkDiscount mkDiscount)
    {
        return toAjax(mkDiscountService.insertMkDiscountGift(mkDiscount));
    }

    /**
     * 删除活动商品
     */
    @PreAuthorize("@ss.hasPermi('market:discount:remove')")
    @Log(title = "店铺活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/delDiscountGift/{ids}")
    public AjaxResult delDiscountGift(@PathVariable Long[] ids)
    {
        return toAjax(mkDiscountService.deleteMkDiscountGiftByIds(ids));
    }

}
